package com.hqyj.javaGraduation.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.javaGraduation.hotel.dao.HotelSupplierMapper;
import com.hqyj.javaGraduation.hotel.pojo.HotelSupplier;
import com.hqyj.javaGraduation.hotel.service.HotelSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class HotelSupplierServiceImpl implements HotelSupplierService {

    @Autowired(required = false)
    private HotelSupplierMapper hotelSupplierMapper;

    @Override
    public HashMap<String, Object> addHotelSupplier(HotelSupplier hotelSupplier) {
        HashMap<String,Object> map = new HashMap<>();
        int i = hotelSupplierMapper.insert(hotelSupplier);
        if (i>0){
            map.put("code","ok");
            map.put("info","添加供应商成功");
        }else {
            map.put("code","false");
            map.put("info","添加供应商失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> yesOrNoPartner(HotelSupplier hotelSupplier) {
        HashMap<String,Object> map = new HashMap<>();
        if (hotelSupplier.getSupPartner().equals("Yes")|| hotelSupplier.getSupPartner() == "Yes"){
            hotelSupplier.setSupPartner("No");
            int i = hotelSupplierMapper.updateById(hotelSupplier);
            if (i>0){
                map.put("code","ok");
                map.put("info","与供应商取消合作");
            }else {
                map.put("code","false");
                map.put("info","与供应商取消合作失败");
            }

        }else if (hotelSupplier.getSupPartner().equals("No") || hotelSupplier.getSupPartner() == "No"){
            hotelSupplier.setSupPartner("Yes");
            int i = hotelSupplierMapper.updateById(hotelSupplier);
            if (i>0){
                map.put("code","ok");
                map.put("info","与供应商合作成功");
            }else {
                map.put("code","false");
                map.put("info","与供应商合作失败");
            }
        }

        return map;
    }

    @Override
    public HashMap<String, Object> listAll(HotelSupplier hotelSupplier) {
        HashMap<String, Object> map = new HashMap<>();
        /* 第一步：创建分页对象 */
        Page<HotelSupplier> hotelSupplierPage = new Page<>(hotelSupplier.getPage(),hotelSupplier.getRow());

        /* 创建查询条件对象 */
        QueryWrapper<HotelSupplier> queryWrapper = new QueryWrapper<>();
        /*精准条件查询*/
        queryWrapper.eq(hotelSupplier.getSupPartner()!=null && !hotelSupplier.getSupPartner().equals(""),
                "sup_partner",
                hotelSupplier.getSupPartner());

        /* 第二步查询分页，获取分页数据集 */
        Page<HotelSupplier> pageList = hotelSupplierMapper.selectPage(hotelSupplierPage, queryWrapper);
        /* 当前页的集合 */
        map.put("info",pageList.getRecords());
        /* 获取总条数 */
        map.put("total",pageList.getTotal());
        /* 获取总页数 */
        map.put("totalPage",pageList.getPages());

        return map;
    }

    @Override
    public HashMap<String, Object> update(HotelSupplier hotelSupplier) {
        HashMap<String, Object> map = new HashMap<>();
        int i = hotelSupplierMapper.updateById(hotelSupplier);
        if (i>0){
            map.put("code","ok");
            map.put("info","更新供应商信息成功");
        }else {
            map.put("code","false");
            map.put("info","更新供应商信息失败");
        }


        return map;
    }

    @Override
    public HashMap<String, Object> delSupplier(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int i = hotelSupplierMapper.deleteById(id);
        if (i>0){
            map.put("code","ok");
            map.put("info","删除供应商信息成功");
        }else {
            map.put("code","false");
            map.put("info","删除供应商信息失败");
        }

        return map;
    }
}
